package interpreter

import "parse/src/utils"

type String struct {
	*BaseData
	Value string
}

// 实例化
func NewString(value string) *String {
	str := &String{BaseData: NewBaseData()}
	str.InitString(value)
	return str
}

// 初始化string
func (s *String) InitString(value string) {
	s.Value = value
}

// 字符串相加
func (s *String) AddedTo(other interface{}) (interface{}, interface{}) {
	if utils.IsInstance(other, "String") {
		return NewString(s.Value + other.(String).Value).SetContext(s.Context), nil
	} else {
		return nil, s.IllegalOperation(other)
	}
}

// 字符串相乘
func (s *String) MultedBy(other interface{}) (interface{}, interface{}) {
	if utils.IsInstance(other, "Number") {
		strRes := s.Value
		temp := other.(Number).Value.(int64)
		for temp > 0 {
			strRes += s.Value
			temp--
		}
		return NewString(strRes).SetContext(s.Context), nil
	} else {
		return nil, s.IllegalOperation(other)
	}
}

// 字符串相乘
func (s *String) IsTrue() bool {
	return len(s.Value) > 0
}

// 字符串拷贝
func (s *String) Copy() *String {
	copy_ := NewString(s.Value)
	copy_.SetPos(s.PosStart, s.PosEnd)
	copy_.SetContext(s.Context)
	return copy_
}
